package com.netwit.common.utils;

import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

public class YamlUtil {
    private static final Logger logger = LoggerFactory.getLogger(YamlUtil.class);

    public static Map<String, Object> getMap(String yamlSource) {
        try{
            YamlMapFactoryBean yaml = new YamlMapFactoryBean();
            yaml.setResources(new ClassPathResource(yamlSource));
            return yaml.getObject();
        } catch(Exception ex) {
            logger.error("读取yaml出现异常.", ex);
            return null;
        }
    }

    public static Properties getProperties(String yamlSource) {
        try{
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new ClassPathResource(yamlSource));
            return yaml.getObject();
        } catch(Exception ex) {
            logger.error("读取yaml出现异常.", ex);
            return null;
        }
    }
}